package cn.lzm.org.controller;

import cn.lzm.basic.util.ExcelUtil;
import cn.lzm.org.domain.ShopAuditLog;
import cn.lzm.org.service.IShopService;
import cn.lzm.org.domain.Shop;
import cn.lzm.org.query.ShopQuery;
import cn.lzm.basic.util.AjaxResult;
import cn.lzm.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            System.out.println("%%%%%%%%%%%%%%%%%%");
            System.out.println(shop.getRegisterTime());
            System.out.println("%%%%%%%%%%%%%%%%%%");
/*            Date date = new Date();
            shop.setRegisterTime(date);*/
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/s/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
//        shopService.auditLogList();
        PageList<Shop> shopPageList = shopService.queryPage(query);
        System.out.println(shopPageList);
        return shopService.queryPage(query);
    }

    /**
     * 店铺入驻接口
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        shopService.settlement(shop);
        return AjaxResult.me().success();
    }
    /**
     * 审核通过
     * 1.改变店铺状态
     * 2.通过邮件的方式给用户 发送 激活店铺
     * 3.修改日志
     */
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog){
        System.out.println("****************");
//        System.out.println(shopAuditLog.getShopId());
        System.out.println(shopService);
        System.out.println("****************");
        shopService.auditPass(shopAuditLog);

        return AjaxResult.me().success();
    }

    /**
     * 激活店铺
     */
    @GetMapping("/audit/{shopId}")
    public AjaxResult activation(@PathVariable("shopId") Long shopId){
        shopService.activation(shopId);
        return AjaxResult.me().success();
    }

    /**
     * 审核驳回
     * 1.修改审核日志
     * 2.通过邮件通知用户审核被驳回 并且 返回修改连接  需要将
     */
    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog){
        shopService.auditReject(shopAuditLog);

        return AjaxResult.me().success();
    }

    /**
     * 重新入驻
     */
    @PostMapping("/relocate")
    public AjaxResult relocate(@RequestBody Shop shop){
        shopService.relocate(shop);

        return AjaxResult.me().success();
    }

    /**
     * 将shop列表导出
     */
    @GetMapping("export/excel")
    public void exportExcel(HttpServletResponse response){
        List<Shop> shops = shopService.queryAll();
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xlsx",response);
    }

    /**
     * 将Excel表格导入到shop中
     */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file")MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        for (Shop shop : shops) {
            System.out.println(shop);
        }
//        System.out.println(shops);
       shopService.batchAdd(shops);
    }
    @GetMapping("/echarts")
    public Map<String, Object> loadShopEchartsData(){
        return shopService.loadShopEchartsData();
    }
}
